package com.muchfish.reactor.api;

import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;

public class ReactorRetryExample {
    public static void main(String[] args) throws InterruptedException {
        Mono<Object> source = Mono.fromCallable(() -> {
                    throw new RuntimeException("Simulated error");
                })
                //最大重试次数为3次，初始重试间隔为1秒，并且采用指数回退策略，直到达到最大的回退时间（这里是5秒）。
                .retryWhen(Retry.backoff(3, Duration.ofSeconds(1)).maxBackoff(Duration.ofSeconds(5)));

        source.subscribe(
                data -> System.out.println("Received: " + data),
                error -> System.err.println("Error: " + error.getMessage())
        );

        //得多睡会儿，让它跑完最大重试时间
        Thread.sleep(999999);
    }
}



